package Util;

import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class searchjFrame extends JFrame {
	public 
	JPanel searchmain;
	JPanel spanel1;
	JPanel spanel2;
	JPanel spanel3;
	JPanel spanel4;
	JPanel spanel5;
	JPanel spanel6;
	JPanel spanel7;
	JPanel spanel8;
	JButton writer;
	JButton bnum;
	JButton bname;
	JButton press;
	JButton kind;
	JButton price;
	JButton amount;
	JTextField writerfield;
	JTextField bnumfield;
	JTextField bnamefield;
	JTextField pressfield;
	JTextField kindfield;
	JTextField pricefield;
	JTextField amountfield;
	JTextField iswelcomef;
	JTextField myarea;
	
	public searchjFrame(){
		LayoutManager layoutManager = new GridLayout(4,1);
        setLayout(layoutManager);
        setTitle("书店销售系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400, 400, 485, 343);
		setLocationRelativeTo(null);
		searchmain= new JPanel(new GridLayout(4,1));
		bnum =new JButton("按书号查询");
		bnum.setBounds(100, 10, 300, 100);
		
		bnum.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	               String a = null;
	                String bnum =  bnumfield.getText(); 
	                if(bnumfield.getText().equals("")){
	                	JOptionPane.showMessageDialog(null,"查询量不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                PreparedStatement ps = null;
	                String sql="select * from  BOOK where bnum =?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, bnum);
	                ResultSet rs=ps.executeQuery();
	                while(rs.next()){
	                	String bnum1=rs.getString(1);
	                	String bname=rs.getString(2);
	                	String writer=rs.getString(3);
	                	String press=rs.getString(4);
	                	String kind=rs.getString(5);
	                	String price=rs.getString(6);
	                	String iswelcome = rs.getString(7);
	                	String amount =rs.getString(7);
	                	  
	                	  a="bnum："+bnum1 +"banme: "+bname+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price+"iswelcome: "+iswelcome+"amount："+amount;  
	                	  System.out.println(a);
	                	  result_interface result_interface = new result_interface(a.toString());
	                	  }
	                	
	               
	                JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                
				



	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	           
	            }
				
			}
			
			
		});
		writer = new JButton("按作者查询");
		writer.setBounds(100, 20, 300, 100);
		writer.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement(); 
	                String a=null;
	                String writer =  writerfield.getText();
	                if(writerfield.getText().equals("")){
	                	JOptionPane.showMessageDialog(null,"查询量不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                PreparedStatement ps = null;
	                String sql="select * from  BOOK where writer =?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, writer);
	                ResultSet rs=ps.executeQuery();
	                while(rs.next()){
	                	String bnum=rs.getString(1);
	                	String bname=rs.getString(2);
	                	String writer1=rs.getString(3);
	                	String press=rs.getString(4);
	                	String kind=rs.getString(5);
	                	String price=rs.getString(6);
	                	String iswelcome =rs.getString(7);
	                	String amount =rs.getString(8);
	                	
	                	  a="bnum："+bnum+"banme: "+bname+"writer； "+writer1+"press: "+press+"kind: "+kind+"price:"+price+"iswelcome: "+iswelcome+"amount："+amount;  
	                	  System.out.println(a);
	                	  result_interface result_interface = new result_interface(a.toString());
	                }
	                
	               
	                JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                




	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	           
	            }
				
			}
			
		});
		bname =new JButton("按书名查询");
		bname.setBounds(100, 40,300, 100);
		bname.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){
					String a=null;
	                java.sql.Statement st = conn.createStatement();
	                String bname =  bnamefield.getText();
	                if( bnamefield.getText().equals("")){
	                	JOptionPane.showMessageDialog(null,"查询量不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                PreparedStatement ps = null;
	                String sql="select * from  BOOK where bname =?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, bname);
	                ResultSet rs=ps.executeQuery();
	                while(rs.next()){
	                	String bnum=rs.getString(1);
	                	String bname1=rs.getString(2);
	                	String writer=rs.getString(3);
	                	String press=rs.getString(4);
	                	String kind=rs.getString(5);
	                	String price=rs.getString(6);
	                	String iswelcome =rs.getString(7);
	                	String amount =rs.getString(8);
	                	
	                	  a="bnum："+bnum+"banme: "+bname1+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price+"iswelcome: "+iswelcome+"amount："+amount;  
	                	  System.out.println(a);
	                	  result_interface result_interface = new result_interface(a.toString());
	                	
	                }
	                
	                JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                




	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	           
	            }
			}
			
		});
		
		press=new JButton("按出版社查询");
		press.setBounds(100, 50, 300, 100);
		press.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                String press =  pressfield.getText();
	                if(pressfield.getText().equals("")){
	                	JOptionPane.showMessageDialog(null,"查询量不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                String a=null;
	                PreparedStatement ps = null;
	                String sql="select * from  BOOK where press =?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, press);
	                ResultSet rs=ps.executeQuery();
	                while(rs.next()){
	                	String bnum=rs.getString(1);
	                	String bname=rs.getString(2);
	                	String writer=rs.getString(3);
	                	String press1=rs.getString(4);
	                	String kind=rs.getString(5);
	                	String price=rs.getString(6);
	                	String iswelcome =rs.getString(7);
	                	String amount =rs.getString(8);
	                	 
	                	  a="bnum："+bnum+"banme: "+bname+"writer； "+writer+"press: "+press1+"kind: "+kind+"price:"+price+"iswelcome: "+iswelcome+"amount："+amount;  
	                	  System.out.println(a);
	                	  result_interface result_interface = new result_interface(a.toString());
	                	
	                }
	               
	                JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                




	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	           
	            }
			}
			
		});
		kind=new JButton("按书类查询");
		kind.setBounds(100, 60, 300, 100); 
		
		kind.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                String kind  =   kindfield.getText();
	                if( kindfield.getText().equals("")){
	                	JOptionPane.showMessageDialog(null,"查询量不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                PreparedStatement ps = null;
	                String a=null;
	                String sql="select * from  BOOK where kind =?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, kind);
	                ResultSet rs=ps.executeQuery();
	                while(rs.next()){
	                	String bnum=rs.getString(1);
	                	String bname=rs.getString(2);
	                	String writer=rs.getString(3);
	                	String press=rs.getString(4);
	                	String kind1=rs.getString(5);
	                	String price=rs.getString(6);
	                	String iswelcome =rs.getString(7);
	                	String amount =rs.getString(8);
	                	 
	                	  a="bnum："+bnum+"banme: "+bname+"writer； "+writer+"press: "+press+"kind: "+kind1+"price:"+price+"iswelcome: "+iswelcome+"amount："+amount;  
	                	 System.out.println(a); 
	                	 result_interface result_interface = new result_interface(a.toString());
	                }
	               
	                JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                
	                // new results_interface("查询结果： ",a.toString());




	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	           
	            }
			}
			
		});
		price=new JButton("按价格查询");
		price.setBounds(100, 70, 300, 100);
		price.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                String price  =   pricefield.getText();
	                if(  pricefield.getText().equals("")){
	                	JOptionPane.showMessageDialog(null,"查询量不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                PreparedStatement ps = null; 
	                String a=null;
	                String sql="select * from  BOOK where price =?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, price);
	                ResultSet rs=ps.executeQuery();
	                while(rs.next()){
	                	String bnum=rs.getString(1);
	                	String bname=rs.getString(2);
	                	String writer=rs.getString(3);
	                	String press=rs.getString(4);
	                	String kind=rs.getString(5);
	                	String price1=rs.getString(6);
	                	String iswelcome =rs.getString(7);
	                	String amount =rs.getString(8);
	                	
	                	  a="bnum："+bnum+"banme: "+bname+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price1+"iswelcome: "+iswelcome+"amount："+amount; 
	                	 System.out.println(a); 
	                	 result_interface result_interface = new result_interface(a.toString());
	                	
	                }
	               
	                JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                




	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	           
	            }
			}
			
		});
		price=new JButton("按是否受欢迎查询");
		price.setBounds(100, 80, 300, 100);
		price.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                String iswelcome  =   iswelcomef.getText();
	                PreparedStatement ps = null;
	                String a=null;
	                String sql="select * from  BOOK where iswelcome =?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1, iswelcome);
	                ResultSet rs=ps.executeQuery();
	                while(rs.next()){
	                	String bnum=rs.getString(1);
	                	String bname=rs.getString(2);
	                	String writer=rs.getString(3);
	                	String press=rs.getString(4);
	                	String kind=rs.getString(5);
	                	String price=rs.getString(6);
	                	
	                	String iswelcome1 = rs.getString(7);
	                	String amount =rs.getString(8);
	                	
	                	  a="bnum："+bnum+"banme: "+bname+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price+"iswelcome: "+iswelcome1+"amount："+amount;  
	                	  System.out.println(a);
	                	result_interface result_interface = new result_interface(a.toString());
	                }
	                
	                JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                




	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	           
	            }
			}
			
		});
		amount=new JButton("按数量查询");
		amount.setBounds(100, 90, 300, 100);
		amount.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try (java.sql.Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                String amount  =    amountfield.getText(); 
	                if( amountfield.getText().equals("")){
	                	JOptionPane.showMessageDialog(null,"查询量不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                String a=null;
	                PreparedStatement ps = null;
	                String sql="select * from  BOOK where amount =?";
	                ps=conn.prepareStatement(sql);
	                ps.setString(1,  amount);
	                ResultSet rs=ps.executeQuery();
	                while(rs.next()){
	                	String bnum=rs.getString(1);
	                	String bname=rs.getString(2);
	                	String writer=rs.getString(3);
	                	String press=rs.getString(4);
	                	String kind=rs.getString(5);
	                	String price=rs.getString(6);
	                	String iswelcome = rs.getString(7);
	                	String amount1 =rs.getString(8);
	                	
	                	  a="bnum："+bnum+"banme: "+bname+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price+"iswelcome: "+iswelcome+"amount："+amount1; 
	                	 System.out.println(a);
	                	    result_interface result_interface = new result_interface(a.toString());
	                }
	            
	                JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
	                




	                
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	           
	            }
			}
			
		});
	spanel1=new JPanel();
	spanel2 =new JPanel();
	spanel3=new JPanel();
	spanel4 =new JPanel();
	spanel5=new JPanel();
	spanel6 =new JPanel();
	spanel7=new JPanel();
	bnumfield=new JTextField(8);
	bnamefield=new JTextField(8);
	writerfield=new JTextField(8);
	pricefield=new JTextField(8);
	kindfield=new JTextField(8);
	pressfield=new JTextField(8);
	amountfield =new JTextField(8);
	iswelcomef=new JTextField(8);
	spanel1.add(bnum);
	spanel1.add(bnumfield);
	spanel2.add(writer);
	spanel2.add(writerfield);
	spanel3.add(bname);
	spanel3.add(bnamefield);
	spanel4.add(press);
	spanel4.add(pressfield);
	spanel5.add(kind);
	spanel5.add(kindfield);
	spanel6.add(price);
	spanel6.add(pricefield);
	spanel7.add(amount);
	spanel7.add(amountfield);
	add(spanel1);
	add(spanel2);
	add(spanel3);
	add(spanel4);
	add(spanel5);
	add(spanel6);
	add(spanel7);
	
	setVisible(true);
	}
	 
 
	
			
	
	
	
	

}
